using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;

namespace eGov.SharePoint.MyPeeps.WebParts
{
    [Guid("8fbbdd8d-c450-45ac-b3e5-d4f2792512b6")]
    public class FollowerRequests : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myListName = null;
        private string _domain = null;
        private string _CSPFollowerRequests = "~/_controltemplates/eGov/FollowerRequests.ascx";
        private string _CSPFollowing = "~/_controltemplates/eGov/Following.ascx";

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Peeps")]
        [WebDisplayName("List Name")]
        [WebDescription("List Name witch contains the follower requests.")]
        public string MyListName
        {
            get
            {
                if (_myListName == null)
                {
                    _myListName = "Follower requests";
                }
                return _myListName;
            }
            set { _myListName = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Peeps")]
        [WebDisplayName("Domain")]
        [WebDescription("Domain")]
        public string Domain
        {
            get
            {
                if (_domain == null)
                {
                    _domain = "SIEENA";
                }
                return _domain;
            }
            set { _domain = value; }
        }

        protected UpdatePanel updatePanel;
        eGov.SharePoint.MyPeeps.WebControls.FollowerRequests frControl;
        eGov.SharePoint.MyPeeps.WebControls.Following following;
        public FollowerRequests()
        {
            this.ExportMode = WebPartExportMode.All;
        }
        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!Page.IsPostBack)
            {
            }

            if (!_error)
            {
                try
                {
                    base.CreateChildControls();

                    updatePanel = new UpdatePanel();
                    updatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;


                    frControl = (eGov.SharePoint.MyPeeps.WebControls.FollowerRequests)this.Page.LoadControl(_CSPFollowerRequests);
                    frControl.ID = "frControl1";
                    frControl.MyListName = MyListName;
                    frControl.Domain = Domain;
                    frControl.FollowerRequestChanged += new System.Web.UI.WebControls.CommandEventHandler(frControl_FollowerRequestChanged);
                    PlaceHolder plFrControl = new PlaceHolder();
                    plFrControl.ID = "plhFrControl";
                    plFrControl.Controls.Add(frControl);
                    //this.Controls.Add(frControl);
                    updatePanel.ContentTemplateContainer.Controls.Add(plFrControl);
                    updatePanel.ContentTemplateContainer.Controls.Add(new LiteralControl("</br>"));

                    following = (eGov.SharePoint.MyPeeps.WebControls.Following)this.Page.LoadControl(_CSPFollowing);
                    updatePanel.ContentTemplateContainer.Controls.Add(following);
                    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
                    trigger.ControlID = frControl.ID;
                    trigger.EventName = "FollowerRequestChanged";
                    updatePanel.Triggers.Add(trigger);

                    this.Controls.Add(updatePanel);

                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        protected void frControl_FollowerRequestChanged(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            following.BindControl();
            //Control plhFrControl = this.updatePanel.ContentTemplateContainer.FindControl("plhFrControl");
            // plhFrControl.Controls.Clear();


            //FollowerRequests newFollowerRequests = (eGov.SharePoint.MyPeeps.WebControls.FollowerRequests)this.Controls..FindControl("frControl1");
            // updatePanel.DataBind();
            //    following = (eGov.SharePoint.MyPeeps.WebControls.Following)updatePanel.TemplateControl.FindControl(following.ID);
            //    following = (eGov.SharePoint.MyPeeps.WebControls.Following)this.Page.LoadControl(_CSPFollowerRequests);
            //base.OnLoad(e);
            //CreateChildControls();
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error && !Page.IsPostBack)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}
